package com.sogeti.mentormatch.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import com.sogeti.mentormatch.model.MentorModelLocator;
	
	import mx.controls.Alert;
	import mx.rpc.IResponder;

	/**
	 * The LoginCommand class is the class which executes the login process. It invokes a backend 
	 * service via a delegate class, and coincidentally invokes a backend spring service for logging
	 * into our application.
	 * 
	 * It implements in this case two Interfaces:
	 * 1: ICommand
	 * 2: IResponder
	 * 
	 * All commands will implement the ICommand interface regardless of whether or not they invoke 
	 * backend services. Some of our Cairngorm Commands may only update the Model, but we want to control
	 * the process by dispatching events. However since they only update the model in the client state, they
	 * would not inheirantly invoke a backend service which would be something like a database call,
	 * a spring method, or hibernate method. Etc. 
	 * 
	 * However because it IS a Cairngorm command it MUST implement the ICommand interface.
	 * 
	 * The ICommand interface gives us the execute() Method.
	 * 
	 * This particluar command though also DOES invoke backend services. In this case the login method 
	 * spring service. Because it is an RPC call it MUST implement the IResponder interface as well.
	 * 
	 * This is because when the delegate gets back the response, either good or bad, it will recognize the 
	 * command as the peice of code to handle the result back to. The delegate will determine if the response 
	 * from the backend is good or bad, and send good results to the result() method, while sending bad results
	 * (Faults) to the fault() method.
	 * 
	 * The IResponder interface gives us the result() and fault() methods.
	 * 
	 * No implicit constructor is really needed.
	 */
	public class LoginCommand implements ICommand, IResponder
	{
		private var mentorModel:MentorModelLocator = MentorModelLocator.getInstance();

		/**
		 * Execute method. Put the code you want to "Do" something here.
		 * 
		 * In this case, we want to execute the delegate, invoke the remote object via the delegate
		 * to execute the "login" method on the backend. There should be a spring service bean
		 * exposed to us that we can invoke, and pass in the appropriate vars. We need to give the delegate
		 * two vars:
		 * 1: username or loginName
		 * 2: password.
		 * 
		 * We got these values from the first from the event, which in fact got them from when the event
		 * was constructed back on the LoginView.mxml class, inside the doLogin function of the script block.
		 * There the loginEvent instance was instantiated, and its two member vars were set from the text
		 * boxes on the form.
		 */
		public function execute(event:CairngormEvent):void
		{
			//For now we are going to 'spoof' the good result and go directly to the 
			//result method.
			result(event);
		}
		
		/**
		 * This is what should execute when we get a good result. 
		 * usually this would be something along the lines of update the model.
		 * In this case, we want to update the model and do a couple things:
		 * 1: We want to set loggedIn to true
		 * 2: We want to set the loggedInUser to the one we get back from the successful login method on
		 * 		the backend.
		 * 3: We want to update the view variable to the correct var, to show the user the screen he/she should
		 * 		be seeing when they are logged in.
		 */
		public function result(data:Object):void
		{
			//Here we are basically saying "yes this person logged in, so set the model var accordingly.
			mentorModel.masterNavigationStateString = MentorModelLocator.MASTER_VIEW_LOGGED_IN;
		}
		
		/**
		 * This is the function which we would execute to display to the user an error message, or perhaps 
		 * determine an error code that has been thrown from the backend. 
		 * In this case we will simply throw up an alert to the user telling them they were not logged in. 
		 */
		public function fault(info:Object):void
		{
			Alert.show("There was a problem logging in. Please try again.");
		}
		
	}
}